#include "mycoin.h"
#include<QDebug>

MyCoin::MyCoin(QString img)
{

    QPixmap pix;
    bool ret=pix.load(img);
    if(!ret)
    {
        QString str=QString("金币图片加载失败，失败的路径为：%1").arg(img);
        qDebug()<<str;
        return;
    }

    this->setFixedSize( pix.width(), pix.height() );
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    //初始化定时器
     this->timer1 = new QTimer(this);
     this->timer2 = new QTimer(this);
    //监听金币翻银币的定时器
    connect(this->timer1,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/res/Coin000%1.png").arg(this->min++);
        bool ret=pix.load(str);
        if(!ret)
        {
            QString str=QString("金币图片加载失败，失败的路径为：%1").arg(str);
            qDebug()<<str;
            return;
        }

        this->setFixedSize( pix.width(), pix.height() );
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->max < this->min) //如果小于最小值，重置最大值，并停止定时器
                {
                    this->min=1;
                    this->timer1->stop();

                }

    });
    //监听银币翻金币的定时器
    connect(this->timer2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/res/Coin000%1.png").arg(this->max--);
        bool ret=pix.load(str);
        if(!ret)
        {
            QString str=QString("金币图片加载失败，失败的路径为：%1").arg(str);
            qDebug()<<str;
            return;
        }

        this->setFixedSize( pix.width(), pix.height() );
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->max < this->min) //如果小于最小值，重置最大值，并停止定时器
                {
                    this->max=8;
                    this->timer2->stop();
                }

    });
}
void MyCoin::changeFlag()
{
    if(this->flag) //如果是正面，执行下列代码
    {
        this->timer1->start(30);
        this->flag = false;
    }
    else //反面执行下列代码
    {
        this->timer2->start(30);
        this->flag = true;
    }

}
void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if(this->isWin )
    {
        return;
    }
    else
    {
        return QPushButton::mousePressEvent(e);
    }
}


